package com.jd.service.impl;

import com.common.model.Result;
import com.gavel.utils.StringUtils;
import com.jd.model.ProductApplyCondition;
import com.jd.model.ProductApplyVO;
import com.jd.model.VCProductInfoVO;
import com.jd.service.JDProductApplyService;
import com.mrobuy.jdvc.dao.VCProductApplyDao;
import com.mrobuy.jdvc.entity.VCProductApply;
import com.mrobuy.jdvc.entity.VCShelvesItem;
import com.mrobuy.type.ItemState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service("jdProductApplyService")
public class JDProductApplyServiceImpl implements JDProductApplyService {

    private static Logger LOG = LoggerFactory.getLogger(JDProductApplyService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private VCProductApplyDao vcProductApplyDao;

    @Override
    public Result<ProductApplyVO> loadProductApplies(ProductApplyCondition condition) {

        Result<ProductApplyVO> result = new Result();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");
        if ( condition.getState() != null ) {
            whereCond.append("  and STATE = ? ");
            params.add(condition.getState());
        }

        if ( condition.getReject() != null ) {
            whereCond.append("  and REJECT = ? ");
            params.add(condition.getReject());
        }

        if ( StringUtils.isNotBlank(condition.getName()) ) {
            whereCond.append("  and NAME like ? ");
            params.add("%" + condition.getName().trim() + "%");
        }

        try {
            String sql = "select * from VC_PRODUCT_APPLY ";

            String cnt_sql = "select count(1)  from VC_PRODUCT_APPLY ";

            int count = jdbcTemplate.queryForObject(cnt_sql + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);


            int fromIndex = (condition.getPage() - 1)*condition.getLimit();
            int toIndex = condition.getPage()*condition.getLimit();

            if ( fromIndex > count ) {
                fromIndex = 0;
                toIndex = 0;
            } else if ( toIndex > count ) {
                toIndex = count;
            }

            params.add(fromIndex);
            params.add(toIndex);
            List<ProductApplyVO> list = jdbcTemplate.query(sql + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(ProductApplyVO.class));

            result.setCode(0);
            result.setCount(count);
            result.setData(list);

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return result;
    }

    @Override
    public void reset(String applyId) {
        VCProductApply apply =  vcProductApplyDao.load(applyId);
        if ( apply==null ) {
            throw new RuntimeException("未找到有效的申请记录.");
        }

        if ( apply.getReject()==null || !apply.getReject() ) {
            throw new RuntimeException("被驳回的商品才能重新上架.");
        }

        if ( apply.getReset()!=null && "Y".equalsIgnoreCase(apply.getReset()) ) {
            throw new RuntimeException("此申请已经标记为重新上架.");
        }

        List<VCShelvesItem> list = jdbcTemplate.query("select * from VC_SHELVES_ITEM where APPLYID = ? ", new BeanPropertyRowMapper(VCShelvesItem.class), applyId);
        if ( list==null || list.size()==0 ) {
            return;
        }

        for (VCShelvesItem item : list) {
            item.setZt(ItemState.INIT.code());
            vcProductApplyDao.update(item);
        }

        apply.setReset("Y");
        apply.setResetTime(Calendar.getInstance().getTime());
        vcProductApplyDao.update(apply);
    }
}
